package cn.lg.soar.system.biz.modules.auth.controller;

import cn.lg.soar.common.model.HttpResult;
import cn.lg.soar.common.model.PageQuery;
import cn.lg.soar.common.model.PageResult;
import cn.lg.soar.core.annotations.IdempotentByParam;
import cn.lg.soar.core.annotations.constraints.Id;
import cn.lg.soar.core.api.IResourceApi;
import cn.lg.soar.core.model.DataScopeInfo;
import cn.lg.soar.database.base.controller.BaseController;
import cn.lg.soar.database.query.QueryParams;
import cn.lg.soar.mvc.annotations.*;
import cn.lg.soar.common.enums.LogTypeEnum;
import cn.lg.soar.system.api.constant.ModuleConst;
import cn.lg.soar.system.biz.modules.auth.entity.Resource;
import cn.lg.soar.system.biz.modules.auth.service.IResourceService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

/**
 * @author luguoxiang
 * 开源项目：https://gitee.com/lgx1992/lg-soar 求star！请给我star！请帮我点个star！
 */
@ConditionalOnProperty(name = "lg.database.enabled-data-permission", havingValue = "true", matchIfMissing = true)
@Validated
@Tag(name = "资源管理")
@RestController
@RequestMapping(ModuleConst.MODULE_NAME + "/resource")
public class ResourceController extends BaseController<Resource, IResourceService> {

    private final IResourceApi resourceApi;

    public ResourceController(IResourceApi resourceApi) {
        this.resourceApi = resourceApi;
    }

    @Override
    protected Serializable getId(Resource entity) {
        return entity.getId();
    }

    private final static String SUBJECT = "资源";

    @DisabledTenantAccess
    @Log(value = "新增" + SUBJECT, type = LogTypeEnum.CREATE)
    @IdempotentByParam("#p0")
    @RequestMapping(path = "create", method = RequestMethod.POST)
    public HttpResult<Serializable> create(@RequestBody @Validated Resource data) {
        return createResult(data);
    }

    @DisabledTenantAccess
    @Log(value = "修改" + SUBJECT, type = LogTypeEnum.UPDATE)
    @RequestMapping(path = "update", method = RequestMethod.POST)
    public HttpResult<Void> update(@RequestBody @Validated Resource data) {
        return updateResult(data);
    }

    @DisabledTenantAccess
    @Log(value = "删除" + SUBJECT, type = LogTypeEnum.REMOVE)
    @RequestMapping(path = "remove", method = RequestMethod.POST)
    public HttpResult<Void> remove(@RequestBody List<Long> ids) {
        return super.removeResult(ids);
    }

    @DisabledTenantAccess
    @RequestMapping(path = "detail", method = RequestMethod.GET)
    public HttpResult<Resource> detail(@RequestParam(name = "id") @Id Long id) {
        return detailResult(id);
    }

    @RequestMapping(path = "query", method = RequestMethod.GET)
    public HttpResult<PageResult<Resource>> query(PageQuery pageQuery, QueryParams params){
       return queryResult(pageQuery, params);
    }

    @DisabledTenantAccess
    @Log(value = "导出" + SUBJECT, type = LogTypeEnum.EXPORT)
    @RequestMapping(path = "export", method = RequestMethod.GET)
    public void exports(@RequestParam(name = "filename", required = false) String filename, QueryParams params, HttpServletResponse resp){
        super.exportExcel(filename, params, resp);
    }

    @DisabledTenantAccess
    @Log(value = "导入" + SUBJECT, type = LogTypeEnum.IMPORT)
    @RequestMapping(path = "import", method = RequestMethod.POST)
    public HttpResult<Void> imports(HttpServletRequest req){
        return super.importExcel(req);
    }

    @DisabledTenantAccess
    @Log(value = "获取所有数据权限注解配置")
    @GetMapping("dataScopeInfos")
    public HttpResult<Map<String, DataScopeInfo>> dataScopeInfos(){
        return HttpResult.data(resourceApi.getAllDataScopeInfo());
    }

    @DisabledTenantAccess
    @Operation(summary = "扫描数据权限配置生成资源")
    @PostMapping(value = "scan", name = "扫描数据权限配置生成资源")
    public HttpResult<Void> scan(){
        Map<String, DataScopeInfo> dataScopeInfoMap = resourceApi.getAllDataScopeInfo();
        this.service.scanDataScopeGenPermit(dataScopeInfoMap.values());
        return HttpResult.success();
    }

    @DisabledTenantAccess
    @Operation(summary = "清空缓存")
    @PostMapping(value = "clear-cache", name = "清空缓存")
    public HttpResult<Void> clearCache(){
        this.service.clearCache();
        return HttpResult.success();
    }

}
